﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Bussiness_Objects;
using DataObjects.ADO;

namespace WindowsFormsApplication1
{
    public partial class TraSach : DevComponents.DotNetBar.Controls.SlidePanel
    {
        DocGia _docGia = new DocGia();
        SqlDocGia _sqlDocGia = new SqlDocGia();

        PhieuMuonSach _phieuMuonSach = new PhieuMuonSach();
        SqlPhieuMuonSach _sqlPhieuMuonSach = new SqlPhieuMuonSach();

        PhieuTraSach _phieuTra = new PhieuTraSach();
        SqlPhieuTraSach _sqlPhieuTra = new SqlPhieuTraSach();

        CTPhieuTraSach _ctPhieuTra = new CTPhieuTraSach();
        SqlCTPhieuTraSach _sqlCTPhieuTra = new SqlCTPhieuTraSach();

        CTPhieuMuonSach _ctPhieMuon = new CTPhieuMuonSach();
        SqlCTPhieuMuonSach _sqlCTPhieuMuon = new SqlCTPhieuMuonSach();
        
        DataTable _sachMuon;

        public TraSach()
        {
            _sachMuon = new DataTable();
            InitializeComponent();
            this.SlideOutButtonVisible = false;
            
        }

        private void UpdateSize()
        {
            this.Size = ((DevComponents.DotNetBar.Metro.MetroForm)MainForm._mainForm).Size;
            grbThongTin.Location = new Point(
                this.Size.Width / 2 - grbThongTin.Size.Width / 2,
                this.Size.Height / 2 - grbThongTin.Size.Height / 2);
        }

        public void ShowSlide()
        {
            ResetField();
            this.IsOpen = true;
            UpdateSize();            
        }

        private void ResetField()
        {
            txtMaDG.Text = null;
            txtTenDG.Text = null;
            dtimeNgayTra.Value = DateTime.Today;
            txtTienNo.Text = "0";
            txtTienPhat.Text = "0";
            txtTienTra.Text = "0";
            txtTongNo.Text = "0";
            panel_Result.Text = "";

            dgvSachMuon.Rows.Clear();
            txtMaDG.Focus();
        }

        public void SetValue(int maDG)
        {
            txtMaDG.Text = maDG.ToString();
            getDocGia();
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.IsOpen = false;
        }

        private void txtMaDG_Enter(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaDG.Text))
                getDocGia();
        }

        private void getDocGia()
        {
            _docGia.MaDocGia = int.Parse(txtMaDG.Text);
            DataTable table = _sqlDocGia.GetById(_docGia);
            if (table.Rows.Count > 0)
            {
                txtTenDG.Text = table.Rows[0]["HoTen"].ToString();
                txtTienNo.Text = table.Rows[0]["TongNo"].ToString();

                _phieuMuonSach.MaDocGia = int.Parse(txtMaDG.Text);
                _sachMuon = _sqlPhieuMuonSach.GetCTMuon(_phieuMuonSach, dtimeNgayTra.Text);
                if (_sachMuon.Rows.Count > 0)
                    createGribView();
                else
                    panel_Result.Text = "Độc giả này không có sách đang mượn";
            }
            else
                panel_Result.Text = "Mã Độc giả không chính xác";
        }

        private void createGribView()
        {
            dgvSachMuon.Rows.Clear();
            int tienphat = 0;
            foreach (DataRow row in _sachMuon.Rows)
            {                
                DateTime date = (DateTime)row["NgayMuon"];
                dgvSachMuon.Rows.Add(new object[] { row["MaCuonSach"], row["TenSach"], date.ToShortDateString(), row["SoNgayTraTre"], row["TienPhat"] , false});
                tienphat += (int)row["TienPhat"];
            }
            txtTienPhat.Text = tienphat.ToString();
        }

        private void txtTienTra_Leave(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtTienTra.Text))
            {
                txtTongNo.Text = (float.Parse(txtTienNo.Text) + int.Parse(txtTienPhat.Text) - int.Parse(txtTienTra.Text)).ToString();
            } 
        }

        private void butNhapDG_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaDG.Text))
                Nhap();
            else
                panel_Result.Text = "Hãy nhập mả độc giả";
        }

        private void Nhap()
        {
            if (_sachMuon.Rows.Count > 0)
            {
                /************************************************************************/
                /* Phieu Tra Sach                                                       */
                /************************************************************************/
                _phieuTra.MaDocGia = int.Parse(txtMaDG.Text);
                _phieuTra.NgayTra = dtimeNgayTra.Text;
                _phieuTra.TienPhatKyNay = long.Parse(txtTienPhat.Text);
                _phieuTra.SoTienTra = long.Parse(txtTienTra.Text);
                _sqlPhieuTra.Insert(_phieuTra);
                /************************************************************************/
                /* Doc gia                                                              */
                /************************************************************************/
                _docGia.TongNo = long.Parse(txtTongNo.Text);
                _sqlDocGia.UpdateTongNo(_docGia);
                //////////////////////////////////////////////////////////////////////////
                if (_sqlPhieuTra.Insert(_phieuTra) == "Success")
                {
                    /************************************************************************/
                    /* CT Phieu Tra Sach                                                    */
                    /************************************************************************/
                    for (int i = 0; i < _sachMuon.Rows.Count; i++)
                    {
                        if ((bool)dgvSachMuon[_traSach.Name, i].Value == true)
                        {
                            _ctPhieuTra.SoPT = _phieuTra.SoPT;
                            _ctPhieuTra.MaCuonSach = int.Parse(_sachMuon.Rows[i]["MaCuonSach"].ToString());
                            _ctPhieuTra.NgayMuon = _sachMuon.Rows[i]["NgayMuon"].ToString();
                            _ctPhieuTra.SoNgayMuon = int.Parse(_sachMuon.Rows[i]["SoNgayMuon"].ToString());
                            _ctPhieuTra.TienPhat = int.Parse(_sachMuon.Rows[i]["TienPhat"].ToString());
                            _sqlCTPhieuTra.Insert(_ctPhieuTra);

                            /************************************************************************/
                            /* CT Phieu Muon Sach Update Tinh Trang                                 */
                            /************************************************************************/
                            _ctPhieMuon.SoPM = int.Parse(_sachMuon.Rows[i]["SoPM"].ToString());
                            _ctPhieMuon.MaCuonSach = int.Parse(_sachMuon.Rows[i]["MaCuonSach"].ToString());
                            _sqlCTPhieuMuon.UpdateTinhTrang(_ctPhieMuon);                                                        
                        }
                    }
                }
                ResetField();
                panel_Result.Text = "Đã nhập thành công";
            }
            else
                panel_Result.Text = "Độc giả này không có sách đang mượn";
        }

        private void txtTienTra_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsPunctuation(e.KeyChar))
                e.Handled = true;
        }

        private void butNhapLai_Click(object sender, EventArgs e)
        {
            ResetField();
            txtMaDG.Focus();
        }

        private void txtMaDG_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode ==  Keys.Enter)
                if (!string.IsNullOrEmpty(txtMaDG.Text))
                    getDocGia();
        }

        private void dtimeNgayTra_TextChanged(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtMaDG.Text))
                getDocGia();
        }

        private void txtMaDG_TextChanged(object sender, EventArgs e)
        {
            panel_Result.Text = "";
        }

        private void txtTienTra_TextChanged(object sender, EventArgs e)
        {
            txtTienTra_Leave(sender, e);
        }
    }
}
